package grp.test.service.Impl;

import java.sql.Date;
import grp.test.dao.BorrowMapper;
import grp.test.pojo.Book;
import grp.test.dao.BookMapper;
import grp.test.pojo.Borrow;
import grp.test.pojo.BorrowKey;
import grp.test.service.IBorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by 明婵度 on 2020/8/15.
 */
@Service("iBorrowService")
public class BorrowServiceImpl implements IBorrowService{
    @Autowired(required = false)
    private BorrowMapper borrowmapper;
    @Autowired(required = false)
    private BookMapper bookmapper;
    @Override
    public Book SearchBookById(Integer bookId) {
        Book bookobj = bookmapper.selectByPrimaryKey(bookId);
        if(bookobj == null){
            return null;
        }
        return bookobj;
    }
    @Override
    public Book SearchBookByName(String bookName) {
        Book bookobj = bookmapper.selectByBookName(bookName);

        if(bookobj == null){
            return null;
        }
        return bookobj;
    }

    @Override
    public List<String> BorrowBook(Integer bookId,Integer userId) {
        Date borrowDate = new Date(System.currentTimeMillis());
        Date backDateSet = new Date(System.currentTimeMillis());
        List<String> result = new ArrayList<>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        sdf.format(borrowDate);//获取当前系统时间
        Calendar calendar = Calendar.getInstance();//日历对象
        calendar.setTime(borrowDate);//设置当前时间
        calendar.add(Calendar.DAY_OF_MONTH, 30);//加减day时间
        java.util.Date udate = (java.util.Date)calendar.getTime();
        Date sqldate = new Date(udate.getTime());
        backDateSet= sqldate;//返回格式为date类型

        Book bookobj = bookmapper.selectByPrimaryKey(bookId);
        int store = bookobj.getStore();
        if(store>=1) {
            Borrow record = new Borrow(userId, bookId, borrowDate, backDateSet, backDateSet, 0.0);
            bookobj.setStore(bookobj.getStore()-1);
            int sub = bookmapper.updateStore(bookobj);
            int flag = borrowmapper.insert(record);
            if(flag==1)
            {
                result.add(bookId.toString());
                result.add(borrowDate.toString());
                result.add(backDateSet.toString());
                result.add("borrow successful");
                return result;

            }
            else {
                result.add("Out of stock");
                return result;
            }
        }
        else
        {
            result.add("Out of stock");
            return result;
        }
    }

    //续借
    @Override
    public Borrow RenewBook(BorrowKey user)
    {
        Borrow obj = borrowmapper.selectByPrimaryKey(user);
        Date date = obj.getBackDateSet();
        System.out.print(date);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        sdf.format(date);//获取时间
        Calendar calendar = Calendar.getInstance();//日历对象
        calendar.setTime(date);//设置时间
        calendar.add(Calendar.DAY_OF_MONTH, 30);//加减day时间
        java.util.Date utilDate = (java.util.Date)calendar.getTime();
        Date sqldate = new Date(utilDate.getTime());
        obj.setBackDateReal(sqldate); //返回格式为date类型
        borrowmapper.updateByPrimaryKey(obj);

        Borrow objnew = borrowmapper.selectByPrimaryKey(user);
        return objnew;

    }


//查询借阅记录
    @Override
    public Borrow LookMine(BorrowKey user) {
        Borrow obj = borrowmapper.selectByPrimaryKey(user);
        if(obj == null){
            return null;
        }
        return obj;

    }
}
